﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace CYTS.Aoyou.Framework.Utility
{
    /// <summary>
    /// 验证帮助类
    /// </summary>
    internal sealed class Validator
    {
        //是否验证空格，如果是：则空格被视为无效(不通过)字符
        private static readonly bool _checkBlank = true;

        /// <summary>
        /// 验证是否为int类型
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否</returns>
        public static bool IsInt32(string value)
        {
            bool result = false;

            //验证是否为空
            if (value != null)
            {
                //验证长度，int32最大长度为11
                if (CheckLength(value, 11))
                {
                    int temp = 0;

                    result = int.TryParse(value, out temp);
                }
            }

            return result;
        }

        /// <summary>
        /// 验证是否为int类型
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否</returns>
        public static bool IsInt32(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsInt32(value.ToString());
            }

            return result;
        }

        /// <summary>
        /// 验证是否为bool类型
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否</returns>
        public static bool IsBoolean(string value)
        {
            bool result = false;

            if (value != null)
            {
                if (CheckLength(value, 5))
                {
                    if (NumberIsTrue(value))
                    {
                        result = true;
                    }
                    else
                    {
                        bool temp = false;

                        result = bool.TryParse(value, out temp);
                    }
                }
            }

            return result;
        }

        /// <summary>
        /// 验证数字是否为真
        /// (1:true;否则:false)
        /// </summary>
        /// <param name="number"></param>
        /// <returns></returns>
        internal static bool NumberIsTrue(string number)
        {
            bool result = false;

            if (number != null)
            {
                if (number == "1")
                {
                    result = true;
                }
            }

            return result;
        }

        /// <summary>
        /// 验证是否为bool类型
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否</returns>
        public static bool IsBoolean(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsBoolean(value.ToString());
            }

            return result;
        }

        /// <summary>
        /// 验证是否为DateTime类型
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否</returns>
        public static bool IsDateTime(string value)
        {
            if (value == null)
            {
                return false;
            }

            if (_checkBlank)
            {
                if (value.Trim().Length < value.Length)
                {
                    return false;
                }
            }

            DateTime temp = new DateTime();

            bool result = DateTime.TryParse(value, out temp);

            return result;
        }

        /// <summary>
        /// 验证是否为DateTime类型
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否</returns>
        public static bool IsDateTime(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsDateTime(value.ToString());
            }

            return result;
        }

        /// <summary>
        /// 验证是否为short(Int16)类型.
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsInt16(string value)
        {
            bool result = false;

            //验证是否为空
            if (value != null)
            {
                //int16最大长度为6
                if (CheckLength(value, 6))
                {
                    short temp = 0;

                    result = short.TryParse(value, out temp);
                }
            }

            return result;
        }

        /// <summary>
        /// 验证是否为short(Int16)类型.
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsInt16(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsInt16(value.ToString());
            }

            return result;
        }

        /// <summary>
        /// 验证是否为long(Int64)类型.
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsInt64(string value)
        {
            bool result = false;

            //验证是否为空
            if (value != null)
            {
                //int16最大长度为6
                if (CheckLength(value, 20))
                {
                    long temp = 0;

                    result = long.TryParse(value, out temp);
                }
            }

            return result;
        }

        /// <summary>
        /// 验证是否为long(Int64)类型.
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsInt64(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsInt64(value.ToString());
            }

            return result;
        }

        /// <summary>
        /// 验证是否为数值类型
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsNumeric(string value)
        {
            if (value == null)
            {
                return false;
            }

            return Regex.IsMatch(value, @"^[-]?[0-9]*[.]?[0-9]*$");
        }

        /// <summary>
        /// 验证是否为数值类型
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static bool IsNumeric(object value)
        {
            if (value == null)
            {
                return false;
            }

            return IsNumeric(value.ToString());
        }

        public static bool IsDouble(string value)
        {
            if (value == null)
            {
                return false;
            }

            if (_checkBlank)
            {
                if (value.Trim().Length < value.Length)
                {
                    return false;
                }
            }

            double temp = 0;

            bool result = double.TryParse(value, out temp);

            return result;
        }

        public static bool IsDouble(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsDouble(value.ToString());
            }

            return result;
        }

        public static bool IsFloat(string value)
        {
            if (value == null)
            {
                return false;
            }

            if (_checkBlank)
            {
                if (value.Trim().Length < value.Length)
                {
                    return false;
                }
            }

            float temp = 0;

            bool result = float.TryParse(value, out temp);

            return result;
        }

        public static bool IsFloat(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsFloat(value.ToString());
            }

            return result;
        }

        public static bool IsDecimal(string value)
        {
            if (value == null)
            {
                return false;
            }

            if (_checkBlank)
            {
                if (value.Trim().Length < value.Length)
                {
                    return false;
                }
            }

            decimal temp = 0;

            bool result = decimal.TryParse(value, out temp);

            return result;
        }

        public static bool IsDecimal(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsDecimal(value.ToString());
            }

            return result;
        }

        public static bool IsChar(string value)
        {
            bool result = false;

            //验证是否为空
            if (value != null)
            {
                //char最大长度为1
                if (CheckLength(value, 1))
                {
                    char temp = '\0';

                    result = char.TryParse(value, out temp);
                }
            }

            return result;
        }

        public static bool IsChar(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsChar(value.ToString());
            }

            return result;
        }

        public static bool IsByte(string value)
        {
            bool result = false;

            //验证是否为空
            if (value != null)
            {
                //byte最大长度为4(16进制)
                if (CheckLength(value, 4))
                {
                    byte temp = 0;

                    result = byte.TryParse(value, out temp);
                }
            }

            return result;
        }

        public static bool IsByte(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsByte(value.ToString());
            }

            return result;
        }

        public static bool IsTimeSpan(string value)
        {
            if (value == null)
            {
                return false;
            }

            if (_checkBlank)
            {
                if (value.Trim().Length < value.Length)
                {
                    return false;
                }
            }

            TimeSpan temp = new TimeSpan();

            bool result = TimeSpan.TryParse(value, out temp);

            return result;
        }

        public static bool IsTimeSpan(object value)
        {
            bool result = false;

            if (value != null)
            {
                result = IsTimeSpan(value.ToString());
            }

            return result;
        }

        private static bool CheckLength(string value, int maxLength)
        {
            if (value == null)
                return false;

            if (maxLength < 0)
                return false;

            if (_checkBlank)
            {
                if (value.Length > maxLength)
                {
                    return false;
                }

                if (value.Trim().Length < value.Length)
                {
                    return false;
                }
            }
            else
            {
                if (value.Trim().Length > maxLength)
                {
                    return false;
                }
            }

            return true;
        }

        public static bool IsValidLength(string value, int maxLength)
        {
            if (value == null)
            {
                return false;
            }

            if (maxLength < 0)
            {
                return false;
            }

            return IsValidLength(value, 0, maxLength);
        }

        public static bool IsValidLength(string value, int minLength, int maxLength)
        {
            if (value == null)
            {
                return false;
            }

            if (minLength < 0 || maxLength < 0 || minLength > maxLength)
            {
                return false;
            }

            bool result = false;

            if (value.Length >= minLength && value.Length <= maxLength)
            {
                result = true;
            }

            return result;
        }

        /// <summary>
        /// 验证是否为IP
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsIP(string value)
        {
            if (value == null)
            {
                return false;
            }

            return Regex.IsMatch(value, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
        }

        /// <summary>
        /// 验证是否为Email
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsEmail(string value)
        {
            if (value == null)
            {
                return false;
            }

            return Regex.IsMatch(value, @"^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$", RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 验证是否为URL
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsURL(string value)
        {
            if (value == null)
            {
                return false;
            }

            return Regex.IsMatch(value, @"^(http|https)\://([a-zA-Z0-9\.\-]+(\:[a-zA-Z0-9\.&%\$\-]+)*@)*((25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])|localhost|([a-zA-Z0-9\-]+\.)*[a-zA-Z0-9\-]+\.(com|edu|gov|int|mil|net|org|biz|arpa|info|name|pro|aero|coop|museum|[a-zA-Z]{1,10}))(\:[0-9]+)*(/($|[a-zA-Z0-9\.\,\?\'\\\+&%\$#\=~_\-]+))*$", RegexOptions.IgnoreCase);
        }

        /// <summary>
        /// 验证是否为邮政编码
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsZipCode(string value)
        {
            if (value == null)
            {
                return false;
            }

            return Regex.IsMatch(value, @"^[1-9]\d{5}$");
        }

        /// <summary>
        /// 验证是否为手机号码
        /// </summary>
        /// <param name="value">待验证的值</param>
        /// <returns>返回 true:是;false:否.</returns>
        public static bool IsMobile(string value)
        {
            if (value == null)
            {
                return false;
            }

            return Regex.IsMatch(value, @"^1\d{10}$");
        }
    }
}
